package com.fbee.smartcommunities.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fbee.smartcommunities.entity.Repair;
import com.fbee.smartcommunities.entity.Visitor;
import com.fbee.smartcommunities.mapper.RepairMapper;
import com.fbee.smartcommunities.service.RepairService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fbee.smartcommunities.service.UploadFileService;
import com.fbee.smartcommunities.utils.LocalDateTimeUtil;
import com.fbee.smartcommunities.utils.StringUtil;
import io.minio.errors.*;
import org.springframework.format.datetime.joda.LocalDateTimeParser;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.time.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 汪佳泉
 * @since 2024-08-21
 */
@Service
public class RepairServiceImpl extends ServiceImpl<RepairMapper, Repair> implements RepairService {

    @Resource
    private RepairMapper repairMapper;

    @Resource
    private UploadFileService uploadFileService;

    @Override
    public Page<Repair> getPage(Integer pageNo, Integer pageSize, String keyword, Boolean order,Boolean part) {
        if(keyword != null) keyword = keyword.trim();
        Page<Repair> repairPage = new Page<>(pageNo, pageSize);
        QueryWrapper<Repair> repairWrapper = new QueryWrapper<>();
        repairWrapper.eq("deleted",0);
        if(order){
            repairWrapper.orderByDesc("repair_time");
        }
        if(part != null && part){
            repairWrapper.eq("state", 0);
        } else if(part != null && !part) {
            repairWrapper.eq("state", 1);
        }
        if(keyword != null){
            repairWrapper.like("phone",keyword).or().like("type",keyword).or().like("address",keyword);
        }
        return repairMapper.selectPage(repairPage,repairWrapper);
    }

    @Override
    public Boolean changeState(Long id, Boolean state, String key) {
        String col;
        if("alw".equals(key)){
            col = "state";
        }else {
            col = "deleted";
        }
        return this.update(new UpdateWrapper<Repair>().set(col,!state).eq("id",id));
    }

    @Override
    public Repair getById(Long id) {
        Repair repair = repairMapper.selectOne(new QueryWrapper<Repair>().eq("id", id));
        String img = repair.getImg();
        List<String> images = StringUtil.stringToList(img);
        repair.setImgs(images);
        return repair;
    }

    @Override
    public Boolean addRepair(String type, Long time, String content, String contact, String address, MultipartFile[] files , Long openId) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        StringBuilder sb = new StringBuilder();
        for (int i = 0;i < files.length ;i++) {
            String repair = uploadFileService.upload("repair", files[i]);
            sb.append(repair);
            if (i != files.length - 1) {
                sb.append(",");
            }
        }
        LocalDateTime localDateTime = LocalDateTimeUtil.valueToLocalDateTime(time);
        return this.save(new Repair(0,openId,type,localDateTime,content,contact,address,sb.toString()));
    }

    @Override
    public List<Repair> getListByUid(Long uid) {
        List<Repair> list = this.list(new QueryWrapper<Repair>().eq("user_id", uid));
        for (Repair repair : list) {
            repair.setImgs(StringUtil.stringToList(repair.getImg()));
        }
        return list;
    }
}
